using System;
using ProjectSurvival.System;
using QAssetBundle;
using UnityEngine;
using QFramework;
using Random = UnityEngine.Random;

namespace ProjectSurvival
{
    public partial class BasketBall : ViewController
    {
        
        void Start()
        {
            var direction = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)) *
                            Random.Range(Global.SimpleBasketBallSpeed.Value - 2,
                                Global.SimpleBasketBallSpeed.Value + 2);
            SelfRigidbody2D.velocity = direction;
            hurtBox.OnTriggerEnter2DEvent(other =>
            {
                var enemyHurtBox = other.gameObject.GetComponent<HitHurtBox>();
                if (enemyHurtBox)
                {
                    if (enemyHurtBox.Owner.CompareTag("Enemy"))
                    {
                        enemyHurtBox.Owner.GetComponent<Enemy>().Hurt(Global.SimpleBasketBallDamage.Value);
                        if (Random.Range(0f, 1.0f) > 0.5f)
                        {
                            other.attachedRigidbody.velocity = other.NormalizedDirection2DFrom(this) * 5 +
                                                               other.NormalizedDirection2DFrom(Player.Instance) * 10;
                        }
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.SuperBasketBall.RegisterWithInitValue(unlocked =>
            {
                if (unlocked)
                {
                    transform.localScale *= 3;
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void OnCollisionEnter2D(Collision2D other)
        {
            //判断是碰到了x的线还是y的线
            var normal = other.GetContact(0).normal;
            if (normal.x > normal.y)
            {
                var direction = new Vector2(
                    SelfRigidbody2D.velocity.x,
                    Mathf.Sign(SelfRigidbody2D.velocity.y) *
                    Random.Range(
                        Global.SimpleBasketBallSpeed.Value - 2,
                        Global.SimpleBasketBallSpeed.Value +
                        2) *
                    Random.Range(0.5f, 1.5f)
                );
                SelfRigidbody2D.velocity = direction;
                SelfRigidbody2D.angularVelocity = Random.Range(-360, 360);
            }
            else
            {
                var direction = new Vector2(
                    Mathf.Sign(SelfRigidbody2D.velocity.x) *
                    Random.Range(Global.SimpleBasketBallSpeed.Value - 2,
                        Global.SimpleBasketBallSpeed.Value + 2) *
                    Random.Range(0.5f, 1.5f),
                    SelfRigidbody2D.velocity.y
                );
                SelfRigidbody2D.velocity = direction;
                SelfRigidbody2D.angularVelocity = Random.Range(-360, 360);
            }

            AudioKit.PlaySound(Ball_wav.Ball);
            //判断是否是enemy
            var hurtBox = other.gameObject.GetComponent<HitHurtBox>();
            if (hurtBox)
            {
                if (hurtBox.Owner.CompareTag("Enemy"))
                {
                    var damageTime = Global.SuperBasketBall.Value ? Random.Range(2, 3) + 1 : 1;
                    DamageSystem.CalculateDamage(Global.SimpleBombDamage.Value*damageTime, hurtBox.Owner.GetComponent<Enemy>());
                    //hurtBox.Owner.Hurt(Global.SimpleBasketBallDamage.Value);
                }
            }
        }
    }
}